package com.bfxy.rabbit.producer.service;

import com.bfxy.rabbit.producer.enums.BrokerMessageStatusEnum;
import com.bfxy.rabbit.producer.entity.BrokerMessage;
import com.bfxy.rabbit.producer.mapper.BrokerMessageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MessageStoreService {

    @Resource
    private BrokerMessageMapper brokerMessageMapper;

    public int insert(BrokerMessage brokerMessage) {
        return brokerMessageMapper.insert(brokerMessage);
    }

    public void updateStatusByMessageId(String messageId, BrokerMessageStatusEnum brokerMessageStatusEnum) {
        brokerMessageMapper.changeBrokerMessageStatus(messageId, brokerMessageStatusEnum.getCode(), new Date());
    }

    public List<BrokerMessage> fetchTimeOutMessage4Retry(BrokerMessageStatusEnum brokerMessageStatusEnum) {
        return brokerMessageMapper.queryBrokerMessageStatus4Timeout(brokerMessageStatusEnum.getCode());
    }

    public BrokerMessage selectByMessageId(String messageId) {
        return brokerMessageMapper.selectByPrimaryKey(messageId);
    }

    public int updateTryCountByMessageId(String messageId) {
        return brokerMessageMapper.update4TryCount(messageId, new Date());
    }

}
